<template>
  <van-icon
    :color="value?'#3296fa':'#777'"
    :name="value?'star':'star-o'"
    @click="onCollect"
  />
</template>

<script>
import { Toast } from 'vant'
import { PostCollections, DeleteCollections } from '../api/article'
export default {
  name: 'CollectArticle',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    ArtId: {
      type: [Number, Object, String],
      required: true
    }
  },
  data () {
    return {

    }
  },

  created () {

  },

  methods: {
    async onCollect () {
      Toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      try {
        if (this.value) {
        // this.value==true 表示已经收藏 点击取消收藏
          await DeleteCollections(this.ArtId)
        } else {
          await PostCollections(this.ArtId)
        }
        this.$emit('input', !this.value)
        Toast.success('操作成功')
      } catch (err) {
        Toast.success('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
